<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/25
 * Time: 10:58
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\BusBusiness;
use app\common\model\BusBusinessInfo;
use think\Db;

class BusBusinessLogic extends BaseLogic
{

    private $busBusinessModel;

    public function __construct()
    {
        parent::__construct();
        $this->busBusinessModel = new BusBusiness();
    }

    /**
     * 车方列表
     * @param $tempWhere
     * @param int $nowPage
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function busBusinessList($tempWhere, $nowPage = 1){
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }
        if ($tempWhere['mobile']){
            $where[] = ['mobile', 'like', "%".$tempWhere['mobile']."%"];
        }
        if (is_numeric($tempWhere['status'])){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        $count = $this->busBusinessModel->with('info')->where($where)->count();
        if ($count > 0){
            $busBusinessList = $this->busBusinessModel->with('info')->hidden(['update_time', 'password', 'salt'])->where($where)->page($nowPage, self::$pageNum)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $busBusinessList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    /**
     * 车方审核中列表
     * @param $tempWhere
     * @param int $nowPage
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function busBusinessAuditList($tempWhere, $nowPage = 1){
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }
        if ($tempWhere['mobile']){
            $where[] = ['mobile', 'like', "%".$tempWhere['mobile']."%"];
        }
        $where[] = ['status', '=', 3];
        $count = $this->busBusinessModel->with('info')->where($where)->count();
        if ($count > 0){
            $busBusinessList = $this->busBusinessModel->with('info')->hidden(['update_time', 'password', 'salt'])->where($where)->page($nowPage, self::$pageNum)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $busBusinessList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }


    /**
     * 车方信息
     * @param $businessId
     */
    public function busBusinessInfo($businessId){
        if ($businessId){
            $info = $this->busBusinessModel->get($businessId, 'info');
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '信息为空');
        }
    }

    /**
     * 处理车方信息
     * @param $data
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                break;
            case 'edit':
                //编辑审核车方信息
                $validate = new \app\admin\validate\BusBusiness();
                if (!$validate->check($data)) {
                    return ajaxArr(ErrorCode::$PARAMVALIDATE, $validate->getError());
                }
                foreach ($data as $key=>$val){
                    if (empty($val)){
                        unset($data[$key]);
                    }
                }
                Db::startTrans();
                try{
                    $businessRes = $this->busBusinessModel->allowField(true)->save($data,['id' => $data['id']]);
                    $businessInfoModel = new BusBusinessInfo();
                    $infoRes = $businessInfoModel->allowField(true)->save($data,['business_id' => $data['id']]);
                    if (!$businessRes && $infoRes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$BUSINESSINFOERROR, '车方信息修改错误');
                    }
                } catch (\Exception $e){
                    Db::rollback();
                    return ajaxArr(ErrorCode::$EXCEPTION, $e->getMessage());
                }
                Db::commit();
                return ajaxArr(ErrorCode::$SUCCESS, '操作成功');

                break;
            case 'del':
                break;
            default:
                break;
        }
    }

    /**
     * 获取所有商家
     */
    public function getAllBusiness(){
        $list = $this->busBusinessModel->field('id,name')->select();
        if ($list){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '商家列表为空');
        }
    }

}